/**
 * Copyright (C), 2007-2009, 
 * File Name: page_adjust_org.js
 * Encoding: UTF-8
 * Version: 1.0
 * Date: February 22, 2011
 * History:	
 */
 
Ext.namespace("Ext.milan.pagejs.page_privilegegroup");

/**
 * 分组JS
 * 
 * @author 胡作正(zuozhenghu@sina.com)
 * @version Revision: 1.00 Date: February 15, 2011
 */
Ext.milan.pagejs.page_privilegegroup.modalWin = null;
Ext.milan.pagejs.page_privilegegroup.subModalWin = null;
Ext.milan.pagejs.page_privilegegroup.REQUEST_ACTION = "group/privilegeGroupActiontype";

Ext.milan.pagejs.page_privilegegroup.docLoadBeforeCallBack = function() {
};

Ext.milan.pagejs.page_privilegegroup.docOnReadyCallback = function() {
};

/*--------------- common methods -------------------------*/

/**
 * 打开弹出窗口
 * 
 * @param isSub {Boolean} 是否是子弹出窗口
 * @param url 通过该url去请求数据，并填充到弹出窗口中
 * @param title 弹出窗口标题
 * @param config {Object} 显示属性设置
 */
Ext.milan.pagejs.page_privilegegroup.openModalWin = function(isSub, url, title, config) {
	config = config || {};
	config["url"]=url;
	config["title"]=title;
	if(isSub)
		Ext.milan.pagejs.page_privilegegroup.subModalWin = Milan.commonutils.createModelPop(config);
	else
		Ext.milan.pagejs.page_privilegegroup.modalWin = Milan.commonutils.createModelPop(config);
};

/**
 * 关闭弹出窗口
 * 
 * @param isSub {Boolean} 是否是子弹出窗口
 */
Ext.milan.pagejs.page_privilegegroup.closeWin = function(isSub) {
	if(isSub)
		Milan.commonutils.closeModelPop(Ext.milan.pagejs.page_privilegegroup.subModalWin);
	else
		Milan.commonutils.closeModelPop(Ext.milan.pagejs.page_privilegegroup.modalWin);
};

/**
 * 查询分组列表
 */
Ext.milan.pagejs.page_privilegegroup._search = function() {
	Milan.pagejs.page_index.updateCurrentTabByForm("query_privilege_group_list_form");
};

/**
 * 操作完成后的回调操作
 * 
 * @param actionType
 */
Ext.milan.pagejs.page_privilegegroup.operateCallback = function() {
	Ext.milan.pagejs.page_privilegegroup.closeWin(false);
	Ext.milan.pagejs.page_privilegegroup._search();
};

/**
 * 表单验证
 * 
 * @param frm 表单对象
 */
Ext.milan.pagejs.page_privilegegroup._formCheck = function(frm) {
	function focusElement(el) {
		if(el.focus)
			el.focus();
	};
	
	if(frm.appid.value.trim() == "") {
		Milan.commonutils.createDelayNotifyModel("提示信息", "系统应用名称 不能为空！");
		return false;
	}
	if(frm.groupname.value.trim() == "") {
		Milan.commonutils.createDelayNotifyModel("提示信息", "分组名称 不能为空！");
		return false;
	}
	var reg = /^[a-zA-Z0-9_\u4e00-\u9fa5]{1,20}$/;
	if (!reg.test(frm.groupname.value.trim())) {
		Milan.commonutils.createDelayNotifyModel("提示信息", "分组名称只允许为字母、数字、下划线、中文！"); 
		return false;
	}
	
	if(!frm.remark.value.isEmpty() && frm.remark.value.trim().length > 100) {
		Milan.commonutils.createDelayNotifyModel("提示信息", "备注长度不能超过100个字符！");
		return false;
	}
	
	return true
};

/*--------------- business methods -------------------------*/

/**
 * 查看分组详细信息
 * 
 * @param groupId 分组ID
 */
Ext.milan.pagejs.page_privilegegroup.viewDetail = function(groupId) {
	var url = "group/show.do?pgid=" + groupId;
	Ext.milan.pagejs.page_privilegegroup.openModalWin(false, url, "分组详细信息", {width: 500});
};

/**
 * 查看分组内的人员列表
 * 
 * @param groupId 分组ID
 * @param groupName 分组名称
 */
Ext.milan.pagejs.page_privilegegroup.viewUsersInGroup = function(groupId, groupName) {
	var url = "group/usersInGroup.do?pgid=" + groupId;
	Ext.milan.pagejs.page_privilegegroup.openModalWin(true, url, "查看分组【" + groupName + "】内的人员列表", {width: 650});
};

/**
 * 显示“新建分组”页面
 */
Ext.milan.pagejs.page_privilegegroup.showAddForm = function() {
	Ext.milan.pagejs.page_privilegegroup.openModalWin(false, "group/create.do", "新建分组", 
		{width: 600, callback: function(){focusElement('groupName')}});
};

/**
 * 当分组内没有人员时，由“查看分组内的人员列表”跳转到编辑页面
 * @param groupId 分组ID
 */
Ext.milan.pagejs.page_privilegegroup.forward2AddGroupForm = function(groupId) {
	Ext.milan.pagejs.page_privilegegroup.closeWin(true);
	Ext.milan.pagejs.page_privilegegroup.closeWin(false);
	Ext.milan.pagejs.page_privilegegroup.showEditForm(groupId);
}

/**
 * 显示“编辑分组”页面
 * 
 * @param groupId 分组ID
 */
Ext.milan.pagejs.page_privilegegroup.showEditForm = function(groupId) {
	var url = "group/edit.do?pgid=" + groupId;
	Ext.milan.pagejs.page_privilegegroup.openModalWin(false, url, "编辑分组", 
		{width: 600, callback: function(){focusElement('groupName')}}); 
};

/**
 * 保存分组信息
 * 
 * @param formId 表单id
 */
Ext.milan.pagejs.page_privilegegroup.saveInfo = function(formId) {
	if(!Ext.milan.pagejs.page_privilegegroup._formCheck($(formId)))
		return;
	if($(formId)['level'].value == "true") {
		new Milan.widget.Dialog("系统提示", "该分组的权限为受控状态，加入该分组的用户必须在管理员确认后才能生效。", 
			{confirmCallback: function() {
					Ext.milan.pagejs.page_privilegegroup.submitForm(formId);
				}
			}
		);
	}else {
		Ext.milan.pagejs.page_privilegegroup.submitForm(formId);
	}
};

Ext.milan.pagejs.page_privilegegroup.submitForm = function(formId) {
	var selectedUsers = $("selected_users_in_group");
	for(var i = 0; i < selectedUsers.length; i++) {
		selectedUsers[i].selected = true;
	}
					
	Milan.ajaxutility.formSubmit({
		form: formId,
		success: function(response){
			Ext.milan.pagejs.page_privilegegroup.closeWin(false);
			Ext.milan.pagejs.page_privilegegroup._search();
		} 
	});
}

/**
 * 显示“选择人员”弹出框
 */
Ext.milan.pagejs.page_privilegegroup.selectUsers = function(selectItemId) {

	new Milan.widget.Selector({
		orgType:"12",
		title:"选择人员",
		deptUrl:"dept/selectDept.do?",
		quickSelectUrl:"user/quickUser.do?",
		searchUrl:"user/searchUser.do?",
		isRecentEnable :false,
		isUpdateRecentEnable :false,
		isDeptSelEnable:true
	},
	true,
	{
		onSelectedCallBack : function(arr)
		{
			var selectItem = $(selectItemId);
			var isExisted = false;
			if(arr != null && arr.length > 0) {
				for (var i = 0; i<arr.length; i++) {
					for (var x = 0; x < selectItem.length; x++) {
						if (arr[i].id == selectItem[x].value) {
							isExisted=true;
							break;
						}	
					}
					
					if (!isExisted) {
						var optionEl = document.createElement("OPTION");
						optionEl.text = arr[i].name;
						optionEl.value = arr[i].id;
						selectItem.options.add(optionEl);
					}
						
					isExisted = false;
				}
			}  
			
			Ext.milan.pagejs.page_privilegegroup.displayBtn(selectItem);
		}
	});
};

Ext.milan.pagejs.page_privilegegroup.displayBtn = function(selectItem) {
	if(selectItem.length > 0) {
		Element.show('del_user_in_group_btn');
	}else {
		Element.hide('del_user_in_group_btn');
	}
}

/**
 * 移除用户
 */
Ext.milan.pagejs.page_privilegegroup.removeUser = function() {
	var selectedUsers = $("selected_users_in_group");
	if(selectedUsers.length == 0)
		return;
				
	var delUserArray = [];
	for(var i = 0; i < selectedUsers.length; i++) {
		if(selectedUsers[i].selected) {
			delUserArray.push(selectedUsers[i]);
		}
	}
	
	if(selectedUsers.selectedIndex ==-1){
		new Milan.widget.Dialog("信息提示","请选择要移除的组员!");
		return;
	}
			
	new Milan.widget.ConfirmDialog("确认信息", "用户移除该分组后将不再拥有该分组的权限。" + 
		"确认要从分组移除该用户吗？如果您不小心删除，在提交表单前还可以通过重新选择恢复。", {
		confirmCallback: function() {
			for(var j = 0; j < delUserArray.length; j++) {
				selectedUsers.removeChild(delUserArray[j]);
			}
			
			if(selectedUsers.length == 0) 
				Element.hide('del_user_in_group_btn');
		}
	});
};

/**
 * 删除分组
 * 
 * @param groupId 分组ID
 */
Ext.milan.pagejs.page_privilegegroup.deletePrivilegeGroup = function(groupId) {
	var url = "group/delete.do?pgid=" + groupId;
	new Milan.widget.ConfirmDialog("确认信息", "确认要删除该分组么？删除该分组后，该组内原有人员将失去从该组继承的权限。", {
		confirmCallback: function() {
			Milan.ajaxutility.formSubmit({
				url:url,
				success:Ext.milan.pagejs.page_privilegegroup._search
			});
		}
	});
};

/**
 * 显示“编辑分组权限”页面
 * 
 * @param groupId 分组ID
 */
Ext.milan.pagejs.page_privilegegroup.showEditPrivilegeSetForm = function(groupId) {
    var url = "group/checkPer.do?pgid=" + groupId;
	Milan.pagejs.page_index.updateCurrentTabByUrl(url);
};

/**
 * 保存为分组分配的权限
 */
Ext.milan.pagejs.page_privilegegroup.savePrivilegeSetForGroup = function() {
	
}

/**
 * 选择用户
 * 
 * @param chkObj 复选框
 * @param realName 用户姓名
 */
Ext.milan.pagejs.page_privilegegroup.selectUser = function(chkObj) {
	var userContaniner = $("selected_users_in_group");
	if(chkObj.checked) {
		if($("privilegegroup_user_" + chkObj.value)) return;
		var optionEl = document.createElement("option");
			optionEl.id = "privilegegroup_user_" + chkObj.value;
			optionEl.value = chkObj.value;
			optionEl.text = chkObj.actualName;
		userContaniner.add(optionEl);
			
		//显示“移除”按钮
		if($("del_user_in_group_btn").style.display == "none") {
			Element.show('del_user_in_group_btn');
		}	
	}else {
		userContaniner.remove($('privilegegroup_user_' + chkObj.value).index);

		//隐藏“移除”按钮
		if(userContaniner.length == 0) {
			Element.hide('del_user_in_group_btn');
		}
	}
};

/**
 * 选择全部用户
 * 
 * @param checkState 具有全选功能的复选框的选中状态
 */
Ext.milan.pagejs.page_privilegegroup.selectAllUsers = function(checkState) {
	var refChkItems = document.getElementsByName("userChk");
	CheckboxSelectUtils.selectAll(checkState, refChkItems, Ext.milan.pagejs.page_privilegegroup.selectUser);
};

Ext.milan.pagejs.page_privilegegroup.directoryPermission = function(deptid){
	var url = "directory/auth.do?authid=" + deptid+"&authType=3&dirType=2";
	win = Milan.commonutils.createModelPop({
		title:"目录权限",
		url:url,
		height: "600px",
		width : "320px"
	});
}
/*-------------------- tool methods -------------------------*/	
	
/**
 * 显示“选择人员”弹出框
 */
Ext.milan.pagejs.page_privilegegroup.selectGroupUsers = function(selectItemId) {

	new Milan.widget.Selector({
		orgType:"13",
		title:"选择小组人员",
		deptUrl:"pubGroup/selectGroup.do?",
		quickSelectUrl:"user/quickUser.do?",
		searchUrl:"pubGroup/searchUser.do?",
		isRecentEnable :false,
		isUpdateRecentEnable :false,
		isDeptSelEnable:true
	},
	true,
	{
		onSelectedCallBack : function(arr)
		{
			var selectItem = $(selectItemId);
			var isExisted = false;
			if(arr != null && arr.length > 0) {
				for (var i = 0; i<arr.length; i++) {
					for (var x = 0; x < selectItem.length; x++) {
						if (arr[i].id == selectItem[x].value) {
							isExisted=true;
							break;
						}	
					}
					
					if (!isExisted) {
						var optionEl = document.createElement("OPTION");
						optionEl.text = arr[i].name;
						optionEl.value = arr[i].id;
						selectItem.options.add(optionEl);
					}
						
					isExisted = false;
				}
			}  
			
			Ext.milan.pagejs.page_privilegegroup.displayGroupBtn(selectItem);
		}
	});
};

Ext.milan.pagejs.page_privilegegroup.displayGroupBtn = function(selectItem) {
	if(selectItem.length > 0) {
		Element.show('del_user_in_pg_btn');
	}else {
		Element.hide('del_user_in_pg_btn');
	}
}

/**
 * 移除用户
 */
Ext.milan.pagejs.page_privilegegroup.removeGroupUser = function() {
	var selectedUsers = $("selected_users_in_poa");
	if(selectedUsers.length == 0)
		return;
				
	var delUserArray = [];
	for(var i = 0; i < selectedUsers.length; i++) {
		if(selectedUsers[i].selected) {
			delUserArray.push(selectedUsers[i]);
		}
	}
	
	if(selectedUsers.selectedIndex ==-1){
		new Milan.widget.Dialog("信息提示","请选择要移除的人员!");
		return;
	}
			
	for(var j = 0; j < delUserArray.length; j++) {
		selectedUsers.removeChild(delUserArray[j]);
	}
	
	if(selectedUsers.length == 0) 
		Element.hide('del_user_in_pg_btn');
};
	
	
	
	
	